package com.shijiwei.lifthelper.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.adapter.CollectPagerAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sjw on 2015/12/12.
 */
@ContentView(R.layout.fragment_collect)
public class CollectFragment extends BaseFragment {

    @ViewInject(R.id.vp_collect)
    private ViewPager collectPager;
    @ViewInject(R.id.rg_collect)
    private RadioGroup collectGroup;

    private List<Fragment> fragments = new ArrayList<>();
    private CollectPagerAdapter pagerAdapter;

    public static CollectFragment newInstance() {
        CollectFragment fragment = new CollectFragment();
        return fragment;
    }
    @Override
    public void initView() {
        fragments.add(CollectJokeFragment.newInstance());
        fragments.add(CollectCookFragment.newInstance());

        pagerAdapter = new CollectPagerAdapter(getChildFragmentManager(),fragments);
        collectPager.setAdapter(pagerAdapter);

        collectGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_collect_joke:
                        collectPager.setCurrentItem(0);
                        break;
                    case R.id.rb_collect_cook:
                        collectPager.setCurrentItem(1);
                        break;
                }
            }
        });

        collectPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    collectGroup.check(R.id.rb_collect_joke);
                } else if (position == 1){
                    collectGroup.check(R.id.rb_collect_cook);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {

    }
}
